package dto;

// Generated May 6, 2011 4:19:20 PM by Hibernate Tools 3.3.0.GA

import org.hibernate.Query;
import SessionFactory.SessionFactoryUtil;
import java.util.List;
import javax.naming.InitialContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.SessionFactory;
import static org.hibernate.criterion.Example.create;

/**
 * Home object for domain model class ModPrev.
 * @see dto.ModPrev
 * @author Hibernate Tools
 */
public class ModPrevHome {

	private static final Log log = LogFactory.getLog(ModPrevHome.class);

	private final SessionFactory sessionFactory = SessionFactoryUtil.getInstance();


	public void persist(ModPrev transientInstance) {
		log.debug("persisting ModPrev instance");
		try {
			sessionFactory.getCurrentSession().persist(transientInstance);
			log.debug("persist successful");
		} catch (RuntimeException re) {
			log.error("persist failed", re);
			throw re;
		}
	}

	public void attachDirty(ModPrev instance) {
		log.debug("attaching dirty ModPrev instance");
		try {
			sessionFactory.getCurrentSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(ModPrev instance) {
		log.debug("attaching clean ModPrev instance");
		try {
			sessionFactory.getCurrentSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void delete(ModPrev persistentInstance) {
		log.debug("deleting ModPrev instance");
		try {
			sessionFactory.getCurrentSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public ModPrev merge(ModPrev detachedInstance) {
		log.debug("merging ModPrev instance");
		try {
			ModPrev result = (ModPrev) sessionFactory.getCurrentSession()
					.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public ModPrev findById(Integer id) {
		log.debug("getting ModPrev instance with id: " + id);
		try {
			ModPrev instance = (ModPrev) sessionFactory.getCurrentSession()
					.get("dto.ModPrev", id);
			if (instance == null) {
				log.debug("get successful, no instance found");
			} else {
				log.debug("get successful, instance found");
			}
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<ModPrev> findByExample(ModPrev instance) {
		log.debug("finding ModPrev instance by example");
		try {
			List<ModPrev> results = (List<ModPrev>) sessionFactory
					.getCurrentSession().createCriteria("dto.ModPrev").add(
							create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}
	
	
	
	///////////////////////////////////////////////////////////////////  Admin //////////////////////////////////////////////////////////////
	   public boolean deletemod(Integer id)
	{
		boolean result =true;
		try {
			//sessionFactory.getCurrentSession().beginTransaction();
                      System.out.println("this is before query string is ");
			Query q = sessionFactory.getCurrentSession().createQuery("delete from ModPrev mod where mod.jfMembers.memberId=:id");
			//Query q = sessionFactory.getCurrentSession().createQuery("delete from ModPrev as mod inner join mod.jfMembers as mem where mem.memberId=: id");
                          System.out.println("this is after query string is "+q.getQueryString());
			q.setInteger("id",id);
                        q.executeUpdate();
                        System.out.println("query string is "+q.getQueryString());
			sessionFactory.getCurrentSession().getTransaction().commit();
		} catch (Exception e) {
			result=false;
                        e.printStackTrace();
		}
		return result;
	}
}
